package org.kwin.spring.aop.aspectj;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.List;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import org.kwin.spring.aop.aspectj.proxy.Proxy;
import org.kwin.spring.aop.aspectj.proxy.ProxyChain;

/**
 * @author : wangkai
 * @version V1.0
 * @Package org.kwin.spring.aop.aspectj
 * @Description:
 * @date 2021年10月08日 14:26
 **/
public class ProxyFactory {

    
    public static <T, V> V getInstance(V targetObj, List<Proxy> proxyList) {
        return (V) Enhancer.create(targetObj.getClass(), new MethodInterceptor() {
            /**
             * 代理方法, 每次调用目标方法时都会先创建一个 ProxyChain 对象, 然后调用该对象的 doProxyChain() 方法.
             */
            @Override
            public Object intercept(Object targetObject, Method targetMethod, Object[] methodParams, MethodProxy methodProxy) throws Throwable {
                return new ProxyChain(targetObj, targetMethod, methodProxy, methodParams, proxyList).doProxyChain();
            }
        });
    }
    
    

}
